﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Microsoft.MediaCenter.UI;
using MyMoviesBrowserLibrary;

namespace MyMoviesBrowser
{
    public class TrailerMenu : ContentPage
    {
        List<string> parentalRatings = new List<string>(new string[] { "", "Not Rated", "G", "PG", "PG-13", "NC-17", "R" });

        private int? chosenOption;
        private MyMoviesBrowserDataContext ctx;

        public TrailerMenu()
        { }
        public TrailerMenu(int? menuOption)
        {
            ctx = MyMoviesBrowserBusiness.GetNewMyMoviesBrowserDataContext();

            Content = new VirtualList(this, null);

            
            if (menuOption.HasValue)
            {
                chosenOption = menuOption;
                int count = 0;
                switch (menuOption)
                {
                    case 1:
                        var q = from g in ctx.MovieTrailer_Genres
                                orderby g.Genre.GenreDesc
                                select g;

                        foreach (var genre in q.Select(g=>g.Genre).Distinct().OrderBy(g=>g.GenreDesc))
                        {
                            count = q.Count(g => g.GenreId == genre.GenreId);
                            AddCommand(string.Format("{0} ({1})", genre.GenreDesc, count), genre.GenreId);
                        }
                        break;
                    case 2:
                        var ratings = from r in ctx.Ratings
                                 orderby r.RatingDesc
                                 select r;
                        foreach (Rating rating in ratings)
                        {
                            AddCommand(string.Format("{0} ({1})", rating.RatingDesc, rating.MovieTrailers.Count()), rating.RatingId);
                        }
                        break;
                    case 3:
                        var studios = from s in ctx.Studios
                                      orderby s.StudioName
                                      select s;
                        foreach (Studio studio in studios)
                        {
                            AddCommand(string.Format("{0} ({1})", studio.StudioName, studio.MovieTrailers.Count()), studio.StudioId);
                        }
                        break;                    
                    default:
                        break;
                }
            }
            else
            {
                AddCommand("All Titles", 0);
                AddCommand("Genres", 1);
                AddCommand("MPAA Ratings", 2);
                AddCommand("Studios", 3);
                AddCommand("Play Random", 4);
            }
        }

        void command_Invoked(object sender, EventArgs e)
        {
            MenuCommand command = sender as MenuCommand;
            int? id = (int?)command.Data;

            if (!chosenOption.HasValue && id == 4)
            {
                var trailers = ctx.MovieTrailers.ToList();
                Random random = new Random();
                int index = random.Next(0, trailers.Count - 1);
                Trailer trailer = new Trailer(trailers[index], null, null);
                trailer.GetFiles();
                index = random.Next(0, trailer.Files.Count - 1);
                //MyMoviesBrowserBusiness.MarkTrailerAsWatched(trailer,true);
                Application.Current.PlayMedia(trailer.Files[index],trailer);
            }
            else if (chosenOption.HasValue)
            {
                List<Trailer> TrailersList = new List<Trailer>();
                switch (chosenOption.Value)
                {
                    case 1:
                        TrailersList = (from m in ctx.MovieTrailer_Genres
                                where m.GenreId == id
                                orderby m.MovieTrailer.SortTitle
                                select new Trailer(m.MovieTrailer, m.MovieTrailer.MovieTrailer_Genres, m.MovieTrailer.MovieTrailer_Persons)).ToList();

                        break;
                    case 2:
                        TrailersList = (from m in ctx.MovieTrailers
                                        where m.RatingId == id
                                        orderby m.SortTitle
                                select new Trailer(m, m.MovieTrailer_Genres, m.MovieTrailer_Persons)).ToList();
                        break;
                    case 3:
                        TrailersList = (from m in ctx.MovieTrailers
                                        where m.StudioId == id
                                        orderby m.SortTitle
                                        select new Trailer(m, m.MovieTrailer_Genres, m.MovieTrailer_Persons)).ToList();
                        break;
                    default:
                        break;
                }
                Application.Current.GoToGalleryPage(new TrailerGallery(TrailersList));
                return;
            }
            else
            {
                if (id == 0)
                {
                    var q = from m in ctx.MovieTrailers
                            orderby m.SortTitle
                            select new Trailer(m, m.MovieTrailer_Genres, m.MovieTrailer_Persons);
                    Application.Current.GoToGalleryPage(new TrailerGallery(q.ToList()));
                    return;
                }
                Application.Current.GoToTrailerMenu(new TrailerMenu((int?)id));
            }
        }

        private void AddCommand(string desc, object data)
        {
            MenuCommand command = new MenuCommand() { Description = desc, Owner = this, Data = data };
            command.Invoked += new EventHandler(command_Invoked);
            Content.Add(command);
        }

        public override int JumpTo(string displayValue)
        {
            displayValue = displayValue.ToLower();
            List<string> commands = Content.Cast<MenuCommand>().Select(c => c.Description).ToList();
            List<string> temp = new List<string>();
            int x = -1;
            do
            {
                for (int i = 0; i < commands.Count; i++)
                {
                    if (commands[i].ToLower().StartsWith(displayValue))
                    {
                        x = i;
                        break;
                    }
                }

                if (displayValue.Length > 0)
                    displayValue = displayValue.Substring(0, displayValue.Length - 1);
            } while (x < 0 && displayValue.Length > 0);

            if (x >= 0)
                return x;
            return -1;
        }
    }
}
